import os
from dotenv import load_dotenv
from openai import OpenAI

# 加载环境变量
load_dotenv()

# 检查环境变量
token = os.getenv("AI_STUDIO_ACCESS_TOKEN")
if token:
    print("✅ 环境变量配置成功！")
    
    # 测试API连接
    client = OpenAI(
        api_key=token,
        base_url="https://aistudio.baidu.com/llm/lmapi/v3"
    )
    
    try:
        response = client.chat.completions.create(
            model="ernie-4.0-8k",
            messages=[{"role": "user", "content": "请回复'配置成功'来确认连接。"}],
            max_tokens=20
        )
        print("🚀 API连接成功！")
        print("回复:", response.choices[0].message.content)
    except Exception as e:
        print("❌ API连接失败:", e)
else:
    print("❌ 环境变量未设置，请检查 .env 文件")